package com.guchenbo.study.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class HelloWorldAspect {
    @Pointcut(value = "execution(* com.guchenbo.study.aop.UserService.user(..)) || execution(* com.guchenbo.study.aop.UserService.error(..))")
    public void pointcut() {
    }

    // 前置通知
    @Before(value = "pointcut()")
    public void beforeAdvice() {
        System.out.println("===========before advice");
    }

    // 后置最终通知
    @After(value = "pointcut()")
    public void afterFinallyAdvice() {
        System.out.println("===========after finally advice");
    }

    @AfterReturning(value = "pointcut()")
    public void afterReturning() {
        System.out.println("===========after returning advice");
    }

    @AfterThrowing(value = "pointcut()")
    public void afterThrowing() {
        System.out.println("===========after Throwing advice");
    }

    @Around(value = "pointcut()")
    public Object logAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        try {
            System.out.println("before invoke");
            return proceedingJoinPoint.proceed();
        } finally {
            System.out.println("after invoke");
        }
    }
}
